<?php
    $xtpl_report        = new XTemplate("view/report/report.htm");
    
    $title      = "Thống kê doanh thu";    
    $total      = 0;
    $allTotal   = 0;
    $number     = 0;
    $allNumber  = 0;
    $day1       = $_POST['day1'];
    $day1       = ($day1 == 0) ? ($day1 = '00') : $day1;
    $month1     = $_POST['month1'];
    $month1     = ($month1 == 0) ? ($month1 = '00') : $month1;
    $year1      = $_POST['year1'];
    $year1      = ($year1 == 0) ? ($year1 = '0000') : $year1;
    $pre        = $year1 . '-' . $month1 . '-' . $day1;
    
    $day2       = $_POST['day2'];
    $day2       = ($day2 == 0) ? ($day2 = '00') : $day2;
    $month2     = $_POST['month2'];
    $month2     = ($month2 == 0) ? ($month2 = '00') : $month2;
    $year2      = $_POST['year2'];
    $year2      = ($year2 == 0) ? ($year2 = '0000') : $year2;
    $next       = $year2 . '-' . $month2 . '-' . $day2;
    
    if(isset($_POST['submit'])){
        if(($pre == 0)||($next == 0)||($pre > $next)){
            $error  = "Ngày tháng năm không hợp lệ";
        }
        else{
            $sql_o  = "SELECT * FROM orders WHERE date >= '" . $pre . "' AND date <= '" . $next . "'";
            $re_o   = mysql_query($sql_o);
            while($row_o = mysql_fetch_array($re_o)){
                $sql_od = "SELECT * FROM order_detail WHERE id_o='" . $row_o['id_o'] . "'";
                $re_od  = mysql_query($sql_od);
                while($row_od = mysql_fetch_array($re_od)){
                    $total      = $row_od['number'] * $row_od['cost'];
                    $allTotal   = $allTotal + $total;
                    $number     = $number + $row_od['number'];     
                    $allNumber  = $allNumber + $number;
                    
                    $sql_p  = "SELECT * FROM product WHERE id_p='" . $row_od['id_p'] . "'";
                    $re_p   = mysql_query($sql_p);
                    $row_p  = mysql_fetch_array($re_p);
                                                              
                    $sqldate    = strtotime($row_o['date']);
                    $date       = date('d-m-Y', $sqldate);
                    
                    $xtpl_report    -> assign('date', $date);
                    $xtpl_report    -> assign('milkname', $row_p['milkname']);
                    $xtpl_report    -> assign('number', $number);
                    $xtpl_report    -> assign('total', $total);
                    $xtpl_report    -> insert_loop('REPORT.TABLE.TABLES', array('REPORT'=>$row));
                    $total  = 0;
                    $number = 0;         
                }

            }
            $xtpl_report    -> assign('numbers', $allNumber);
            $xtpl_report    -> assign('totals', $allTotal);    
        }
        $xtpl_report    -> parse("REPORT.TABLE");
    }
    
    $xtpl_report        -> assign('error', $error);
    $xtpl_report        -> parse("REPORT");
    $content            = $xtpl_report -> text("REPORT");
?>